package top.i51213.fastaction.util;

import android.app.Application;
import android.content.Context;
import android.text.PrecomputedText;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class IOUtil {
    public static final long AbsTime=1643009725000L/10;
    public static final long StartTime=new Date().getTime()/10;
    public static int index=0;

    public static File createCacheFile(Context context, String fileName){
        return new File(context.getExternalCacheDir(),fileName);
    }

    public static String getFileName(String file){
        int index=file.lastIndexOf("/");
        return file.substring(index+1);
    }
    public static String getFileNameNoExt(String file){
        String fileName=getFileName(file);
        int index=fileName.lastIndexOf('.');
        return index>=0? fileName.substring(0,index):fileName;
    }
    public static String getFilePathNoExt(String file){
        int index=file.lastIndexOf('.');
        return index>=0? file.substring(0,index):file;
    }
    public static String getExt(String file){
        String fileName=getFileName(file);
        int index=fileName.lastIndexOf('.');
        return index>0? fileName.substring(index):"";
    }
    public static String getFileName(File file){
        return file.getName();
    }

    public static String getTimeString(){
        String strDateFormat = "yyyyMMdd_HHmmss";
        DateFormat df = new SimpleDateFormat(strDateFormat, Locale.CHINESE);
        return df.format(new Date());
    }
    public static String getLongTimeString(){
        String strDateFormat = "yyyyMMdd_HHmmss_SSS";
        DateFormat df = new SimpleDateFormat(strDateFormat, Locale.CHINESE);
        return df.format(new Date());
    }
    public static long getRandomTime(){
        return StartTime-AbsTime+index++;
    }
    public static void copyTo(InputStream in, OutputStream out) throws IOException {
        byte[] buff=new byte[1024*64];
        int len;
        while ((len=in.read(buff))>0){
            out.write(buff,0,len);
        }
    }
    public static void copyToAndClose(InputStream in, OutputStream out) throws IOException {
        copyTo(in,out);
        in.close();
        out.close();
    }
}
